import React from 'react';
import { View, Image} from 'react-native';
import { Lt, Color, Label, Dialog, Jpp, Jx, Button } from "./_utils";
import { Net, NET_RI, NET_NT } from "./_net"

// 获取验证码
export class ViewVerify extends React.PureComponent{
    constructor(props){
        super(props);
        this.state = {
            tick:0,
        }
        this.onTick = this.onTick.bind(this);
        this.onClick = this.onClick.bind(this);
    }
    isTicked(){
        return this.state.tick === 0;
    }
    onTick(){
        if(this.isTicked()){
            clearInterval(this.invterval);
            this.invterval = null;
            return;
        }
        let tick = this.state.tick - 1;
        this.setState({tick});
    }
    componentWillUnmount(){
        this.invterval && clearInterval(this.invterval);
    }
    onClick(){
        if(!this.isTicked()){
            return;
        }

        const { onPhone } = this.props;
        const phone = onPhone ? onPhone() : "";
        let str =  Jx.checkInterger(phone);
        if(str){
            Dialog.msg1("手机号" + str);
            return;
        }
        Net.request("sendSMS", {phone}, {NET_NT, NET_RI}).then(r=>{
            if(r.data){
                Dialog.toast(r.data);
                this.setState({tick:120})
                this.invterval = setInterval(this.onTick, 1000);
            }
        })
    }
    render(){
        return <Button.Round style = {{
            width:null,
            borderWidth:0,
            ...this.props.style,
        }}
            pure = {false}
            title = {this.isTicked() ? "获取验证码" : (this.state.tick + "秒")}
            onPress = {this.onClick}
        />
    }
}